<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class People extends MY_Controller {

	//构造函数
	public function __construct()
	{
		parent::__construct();
		$this->load->model('dxdb_model','peo','people');
	}

	//人员列表
	public function people_list()
	{
        $this->config->set_item('url_suffix', '');
        // 载入分页类
        $this->load->library('pagination');
        $perPage = 23;
        //配置项设置
        $config['base_url'] = site_url('people/people_list');
        $config['total_rows'] = $this->db->count_all_results('people');
        $config['per_page'] = $perPage;
        $config['uri_segment'] = 3;
        // $config['enable_query_strings'] = TRUE;
        $config['first_link'] = '第一页';
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        $config['last_link'] = '最后一页';

            //初始化配置
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();
        $offset = $this->uri->segment(3);
        $this->db->limit($perPage, $offset);
		 $data['peoples']= $this->peo->all();
		// $data['peoples'] = $this->peo->all();
		$this->load->view('people/people_list',$data);

	}


    //people search
    public function peo_search()
    {
    	$user = $this->input->get('pname',true);
    	$data['peoples'] = $this->db->get_where('people', "pe_name like '%$user%'")->result_array();
    	$data['links'] = '';
    	$this->load->view('people/people_list',$data);
    }

	//人员添加
	public function people_add()
	{
		if(IS_POST)
		{
			$data = $this->get_people_data();
			$flag = $this->peo->dx_insert($data);
			if($flag) $this->success('people/people_list');
		}
		else
		{
			// 老师所属分类
			$data = array();
			$this->load->model('dxdb_model','cate','category');
			$peop_cates = $this->cate->all();
			$category = channelLevel($peop_cates);
			$data['peop_cates'] = $category[27]['data'];
			//获取所有老师称谓
			$this->load->model('dxdb_model','tit','job_title');
			$data['titles'] = $this->tit->all();
			$this->load->view('people/people_add',$data);
		}
	}

	//人员编辑
	public function people_edit()
	{
    	$eid = $this->uri->segment(3);
    	if(IS_POST)
    	{
    		$data = $this->get_people_data();
    		$flag = $this->peo->dx_update($data,array('pe_id'=>$eid));
    		if($flag) $this->success('people/people_list');
    	}
    	else
    	{
    		// 老师所属分类
    		$data = array();
    		$this->load->model('dxdb_model','cate','category');
    		$peop_cates = $this->cate->all();
    		$category = channelLevel($peop_cates);
    		$data['peop_cates'] = $category[27]['data'];
    		//获取所有老师称谓
    		$this->load->model('dxdb_model','tit','job_title');
    		$data['titles'] = $this->tit->all();
    		$data['info'] = $this->peo->one(array('pe_id'=>$eid));
    		$this->load->view('people/people_edit',$data);
    	}
	}

	//状态修改
	public function people_status()
	{
		$id = intval($this->input->post('id'));
		$state = intval($this->input->post('state'));
		if($state == 1)
		{
		    $flag = $this->peo->dx_update(array('pe_status'=>0),array('pe_id'=>$id));$msg = '操作成功：信息屏蔽!';
		}
		else
		{
		    $flag = $this->peo->dx_update(array('pe_status'=>1),array('pe_id'=>$id));$msg = '操作成功：信息状态正常 :)';
		}
		if($flag) {$arr['status']  = 1;$arr['message']  = $msg;}else { $arr['status']  = 0;$arr['message']  = "操作失败 :(";} 
	    $this->ajax($arr); 
	}

	//人员删除
	public function people_del()
	{
		$id = intval($this->input->post('id'));
		//删除图片
		$flag = $this->peo->dx_delete(array('pe_id'=>$id));
		if($flag)
		{
		  $arr['status']  = 1; $arr['message']  = "删除信息成功 :)";
		}
		else
		{
		   $arr['status']  = 0; $arr['message']  = "操作失败 :(";         
		} 
		$this->ajax($arr); 
	}


	//图片上传处理
	public function sce_img_upload()
	{
		$image = $_POST['name'];//"goods_image"
		$image_path = '../uploads/people';//图片路径
		$info = $this->_upload_img($image,$image_path);
		//缩略图设置   start
		$crop_img = $info['full_path'];
		$thumb_img = $info['file_path'].$info['raw_name'].'_150_150'.$info['file_ext'];
		thumb($crop_img,$thumb_img, 150, 185, 5);//缩略图
		$data = array ();
		$data ['thumb_name'] = $image_path."/".$info['file_name'];
		$data ['src_name'] = base_url().$image_path."/".$info['file_name'];
		$data ['name']      = $info['file_name'];	 
		//整理为json格式
		$this->ajax($data);
	}
	//图片删除
	public function sce_img_del()
	{
		$img_url = '../uploads/people/'.trim($_POST['name']);//"goods_image"
		@unlink($img_url); 
		echo 1;exit();
	}

	//获取post数据
	private function get_people_data()
	{
		$data = array();
		$sec_title = $this->input->post('pe_sec_title');
		$data = array(
			'pe_name'     =>$this->input->post('pe_name'),
			'c_id'        =>$this->input->post('c_id'),
			'pe_sec_title'=>$sec_title,
			'pe_duty'     =>$this->input->post('pe_duty'),
			'pe_degrees'  =>$this->input->post('pe_degrees'),
			'pe_term'     =>$this->input->post('pe_term'),
			'pe_image'    =>$this->input->post('image'),
			'pe_office'   =>$this->input->post('pe_office'),
			'pe_email'    =>$this->input->post('pe_email'),
			'pe_content'  =>$this->input->post('pe_content'),
			'pe_sort'     =>$this->input->post('pe_sort')
	     );
		return $data;
	}
}